import type { ICustomHttpResponse} from "@/simba/types"
import type { IConserve } from "@/simba/types/conserve.d"
import UserApi from "@/sheep/api/member/user";
import ConserveApi from "@/sheep/api/huadada/conserve";
import sheep from "@/sheep";
	
export const useConserve =()=>{
	function onQrcodeScanHandler(uid: string){
		uni.scanCode({
			success: function(res){
				if(uid == res.result){//非严格相等：两边的值转换为同一类型后进行比较
					uni.showToast({
						icon: "none",
						title: "不能添加自己为共同养护人" 
					})
					return;
				}
				sheep.$router.go('/pages/user/conserves/ConservePersonEdit?uid=' + res.result)
				// uni.navigateTo({
				// 	url: '/pages/user/conserves/ConservePersonEdit?uid=' + res.result
				// })
			},
			fail:function(err: any){
				uni.showToast({
					icon: "none",
					title: "扫码失败"
				})
			}
		})
	}

	// const listConserveMemberAll = ref<IConserveMemberList>([])
	// let listConserveMember = ref<IConserveMemberList>([]) // 已同意
	// let listConserveMemberU = ref<IConserveMemberList>([]) //待审批
	async function getUserInfo(uid: String){
	  // 个人信息
	  const { code, data }  = await UserApi.getUserInfoById(uid) as ICustomHttpResponse

	  if(code !== 0){
		  return
	  }
	  
	  return data 
	  // userInfo.value = data as IUserInfo
	};
	
	async function getConserveList(uid: number){
		const {code, data } = await ConserveApi.getConserveList(uid) as ICustomHttpResponse
		
		if(code !== 0){
			return;
		}
		
		// listConserveMemberAll.value = data
		return data
	}
	
	async function getConservePersonInfo(id:number){
		const { code, data } = await ConserveApi.getConserve(id) as ICustomHttpResponse
		
		if(code !== 0){
			return
		}
		
		return data
	}
	
	async function addConserve(formData: IConserve){
		//当前用户申请添加二维码用户，
		const { code } = await ConserveApi.addConserve(formData)  as ICustomHttpResponse
		if(code === 0){
			uni.showToast({
				icon: 'success',
				title: '申请成功',
				duration: 1000
			})
			setTimeout(()=>{
				uni.hideToast()
				sheep.$router.back()
			}, 1000)
		}
	}
	
	async function updateConserve(formData: IConserve){
		console.log('formData', formData)
		//当前用户申请添加二维码用户，
		const { code } = await ConserveApi.updateConserve(formData)  as ICustomHttpResponse
		if(code === 0){
			uni.showToast({
				icon: 'success',
				title: '审批成功',
				duration: 1000
			})
			setTimeout(()=>{
				uni.hideToast()
				
				sheep.$router.back()
			}, 1000)
		}
	}
	
	async function deleteConserve(id: number){
		
		uni.showModal({
			title: '提示',
			content: '确定要删除此养护人吗',
			success: async (res) => {
				if(!res.confirm){
					return
				}
				const { code } = await ConserveApi.deleteConserve(id)  as ICustomHttpResponse
				if(code === 0){
					uni.showToast({
						icon: 'success',
						title: '删除成功',
						duration: 1000
					})
					setTimeout(()=>{
						uni.hideToast()
						sheep.$router.back()
					}, 1000)
				}
			}
		});
			//当前用户添加自己的位置、品种
			// const res = await new Promise((resolve) => {
			// 	uni.showModal({
			// 	  title: '提示',
			// 	  content: '确定要删除此养护人吗',
			// 	  success: (res) => {
			// 		resolve(res.confirm);
			// 	  }
			// 	});
			//   }
			// );
			
			// if(!res){
			// 	return false
			// }
			// const { code } = await ConserveApi.deleteConserve(id)  as ICustomHttpResponse
			
			// if(code === 0){
			// 	return true
			// }
			// return false
		}
	
	return {
		onQrcodeScanHandler, getUserInfo, getConservePersonInfo, getConserveList,
		addConserve, updateConserve, deleteConserve
	}
}